﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;

namespace YF_Music.Core.Converters
{
  public class SliderValueToMarginConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      double sliderValue = (double)value;

      // 获取 Slider 控件
      if (parameter is Slider slider)
      {
        double trackWidth = slider.ActualWidth;
        double min = slider.Minimum;
        double max = slider.Maximum;

        // 避免除以0
        if (max <= min || trackWidth <= 0)
          return new Thickness(0);

        double ratio = (sliderValue - min) / (max - min);
        double left = ratio * trackWidth;

        return new Thickness(left, 0, 0, 0);
      }

      // 若未传入 Slider 作为 parameter，则默认返回0
      return new Thickness(0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotSupportedException();
    }
  }
}
